package cn.hinglo.gameclaw.admin.action;

import cn.hinglo.gameclaw.admin.service.NoticeService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.bean.Notice;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @createTime: 2018/1/8
 * @author: HingLo
 * @description: 公告/通知内容管理
 */
@RestController
@RequestMapping("admin/n/")
public class NoticeAction extends BaseController {
    private final NoticeService noticeService;

    @Autowired
    public NoticeAction(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    /**
     * 公告通知列表
     *
     * @return 通知/公告列表
     */
    @GetMapping("list")
    public Result list() {
        List<Notice> list = noticeService.findAllNoticeService();
        return success(list);
    }

    /**
     * 添加通知/公告信息
     *
     * @param notice        公告或者通知信息
     * @param bindingResult 信息绑定对象
     * @return 返回操作结果
     */
    @PostMapping("add")
    public Result add(@Valid Notice notice, BindingResult bindingResult, @RequestParam("img") MultipartFile img) {
        if (bindingResult.hasErrors()) {
            return error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (img == null) {
            return error("图标不能为null");
        }
        return success(noticeService.addService(notice, img));
    }

    @PutMapping("status")
    public Result status(String id, Boolean status) {
        if (StrUtil.isBlank(id) || status == null) {
            return error("请求参数不正确");
        }
        return result(noticeService.statusService(id, status), "修改失败");

    }


}
